<template>
  <!-- 权限设置 -->
  <div class="quanxian">
    <div class="search">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="选择部门">
          <el-select v-model="value" value-key="key" placeholder="请选择部门" @change="departChange">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onsubmit">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="text" @click="onclear">清空筛选条件</el-button>
        </el-form-item>
      </el-form>
      <el-button class="ebtn" type="warning" @click="depart">新增岗位</el-button>
    </div>
    <el-table class="table" stripe :data="tableData" style="width: 100%" height="580">
      <el-table-column prop="index" label="序号" align="center"></el-table-column>
      <el-table-column prop="roleName" label="岗位名称" align="center"></el-table-column>
      <el-table-column prop="departName" label="部门" align="center"></el-table-column>
      <el-table-column prop="menuList" label="权限" align="center"></el-table-column>
      <el-table-column prop="state" label="状态" align="center"></el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button size="small" type="primary" @click="qianxian(scope.row.id)">设置权限</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pgnt">
      <el-pagination
        background
        layout="prev, pager, next"
        :current-page.sync="current"
        :page-size="pagesize"
        @current-change="handleCurrentChange"
        :total="total"
      ></el-pagination>
    </div>
    <!-- 设置权限弹框 -->
    <el-dialog
      class="dialog"
      title="设置权限"
      :visible.sync="dialogVisible"
      width="60%"
      :append-to-body="true"
    >
      <div class="el-dialog__body" style="height:400px; ">
        <div class="color_box"></div>
        <div class="ttop">
          <el-form :inline="true" :model="formInlines" class="demo-form-inline">
            <el-form-item label="岗位名称">
              <el-input v-model="formInlines.name"></el-input>
            </el-form-item>
            <el-form-item label="部门">
              <el-input v-model="formInlines.departName"></el-input>
            </el-form-item>
            <!-- <el-form-item>
              <el-button>密码重置</el-button>
            </el-form-item>-->
            <el-form-item label="状态">
              <el-switch
                v-model="formInlines.statue"
                active-color="#13ce66"
                inactive-color="#ff4949"
              ></el-switch>
            </el-form-item>
          </el-form>
        </div>
        <table
          class="table"
          checked
          border="1"
          style="width:95%;text-align:center;background-color:#fff;margin-left:10px;border-color:#f5f5f5;height:400px;"
        >
          <thead>
          <tr style="height:48px;">
            <td>序号</td>
            <td>一级权限</td>
            <td>二级权限</td>
            <td>选择</td>
          </tr>
          </thead>
          <tbody style="height:350px;">
          <tr v-for="(item, index) in tableDatas" :key="index">
            <td>{{ index + 1 }}</td>
            <td>{{ item.name }}</td>
            <td>
              <div
                style="height:30px;line-height:30px;cursor:pointer"
                v-for="(items, indexs) in item.tworank"
                :key="indexs"
              >{{ items.name }}
              </div>
            </td>
            <td>
              <div
                style="height:30px;line-height:30px"
                v-for="(items, index) in item.tworank"
                :key="index"
              >
                <el-checkbox
                  v-model="items.state"
                  @change="changess(index,item.menuId,items.menuId,items.state)"
                ></el-checkbox>
              </div>
            </td>
          </tr>
          </tbody>
        </table>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" type="primary" @click="queding">确 定</el-button>
        <el-button size="small" type="info" @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 新增岗位弹框 -->
    <el-dialog title="新增岗位" :append-to-body="true" :visible.sync="dialogVisibles" width="30%">
      <div class="color_box"></div>
      <el-form
        :inline="true"
        :model="formInliness"
        class="demo-form-inline"
        style="text-align:center;"
      >
        <el-form-item label="选择部门">
          <el-select
            v-model="newvalue"
            value-key="key"
            placeholder="请选择"
            @change="newdepartChange"
            style="width:250px;"
          >
            <el-option
              v-for="item in newoptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
          <!-- <el-button type="primary" >新增部门</el-button> -->
        </el-form-item>
        <el-form-item label="选择级别">
          <el-select
            v-model="roleValue"
            value-key="key"
            placeholder="请选择"
            @change="roleChange"
            style="width:250px;"
          >
            <el-option
              v-for="item in roleList"
              :key="item.id"
              :label="item.roleName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="岗位名称">
          <el-input v-model="formInliness.roleName" style="width:250px;"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" type="primary" @click="addRole">确 认</el-button>
        <el-button size="small" type="info" @click="dialogVisibles = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import axios from "axios";

export default {
  data() {
    return {
      checked: false,
      formInline: {
        region: ""
      },
      formInliness: {
        departId: 0,
        fid: 0,
        roleName: "",
        creater: 0,
        state: true,
      },    //新增岗位传参
      tableData: [],//权限列表
      dialogVisible: false,//设置权限弹框
      dialogVisibles: false,//新增岗位弹框
      formInlines: {
        name: "",
        departName: "",
        statue: true
      },//权限详情
      tableDatas: [],//设置权限详情列表
      value: "",//部门查询下拉框选中
      newvalue: "",//新增岗位部门下拉框选中
      roleValue: "",//新增岗位岗位下拉框选中
      //e: "",
      es: 0,//部门查询id变更
      options: [],
      newoptions: [],
      total: 0,
      current: 1,
      pagesize: 10,
      checkedId: [],//设置选中权限
      codeId: 0,//设置权限的id
      roleList: [],//一级权限集合
    };
  },
  created() {
    this.getData(0);
    this.getDatass();
  },
  methods: {
    // 权限列表
    getData(page) {
      var obj = {
        pn: page,
        PerPageSize: this.pagesize,
        key: this.es
      };
      var objs = JSON.stringify(obj);
      axios(
        "/apis/CEOManage.asmx/GetData?index=4&jsondata=" + objs
      ).then(({data}) => {
        var datas = this.$x2js.xml2js(data);
        var list = JSON.parse(datas.string.__text);
        this.total = list.length > 0 ? list[0].count : 0;
        this.tableData = list;
        //console.log(this.total)
      });
    },
    //获取部门
    getDatass() {
      var obj = {id: 4};
      var objs = JSON.stringify(obj);
      axios(
        "/apis/ComApi.asmx/GetData?index=1&jsondata=" + objs
      ).then(({data}) => {
        var datas = this.$x2js.xml2js(data);
        var list = JSON.parse(datas.string.__text);
        this.options = list;
      });
    },
    // 设置权限
    qianxian(e) {
      this.codeId = e;
      this.dialogVisible = true;
      //this.e = e;
      var obj = {codeId: e};
      var objs = JSON.stringify(obj);
      axios("/apis/CEOManage.asmx/GetData?index=5&jsondata=" + objs).then(
        ({data}) => {
          var datas = this.$x2js.xml2js(data);
          var list = JSON.parse(datas.string.__text);
          console.log(list);
          this.tableDatas = list.menuLis;
          this.formInlines.name = list.roleName;
          this.formInlines.departName = list.departName;
          this.formInlines.statue = list.statue;
          this.checkedId = [];
          for (let i = 0; i < list.menuLis.length; i++) {
            const str = [];
            const arr = list.menuLis[i].tworank;
            if (arr.length > 0) {
              for (let k = 0; k < arr.length; k++) {
                if (arr[k].state == true) {
                  str.push(arr[k].menuId);
                }
              }
            }
            this.checkedId.push(str);
          }
          console.log(this.checkedId, "checkedId")
        }
      );
    },
    //岗位新增按钮点击
    depart() {
      this.roleValue = "";
      this.newvalue = "";
      this.formInliness.roleName = "";
      this.newoptions = [];
      for (var i = 0; i < this.options.length; i++) {
        if (this.options[i].value != 1) {
          this.newoptions.push(this.options[i]);
        }
      }
      this.dialogVisibles = true;
      var obj = {};
      var objs = JSON.stringify(obj);
      axios(
        "/apis/ComApi.asmx/GetData?index=2&jsondata=" + objs
      ).then(({data}) => {
        var datas = this.$x2js.xml2js(data);
        var list = JSON.parse(datas.string.__text);
        this.roleList = [];
        for (var i = 0; i < list.length; i++) {
          if (list[i].departCode == null) {
            this.roleList.push(list[i]);
          }
        }
      });

    },
    // 确认设置权限
    queding() {
      var obj = {
        arr: this.checkedId,
        state: this.formInlines.statue,
        codeId: this.codeId
      };

      console.log(this.checkedId, "this.checkedId")

      console.log(this.checkedId)
      var objs = JSON.stringify(obj);
      axios(
        "/apis/CEOManage.asmx/Add?index=2&jsondata=" + objs
      ).then(({data}) => {
        var datas = this.$x2js.xml2js(data);
        var list = JSON.parse(datas.string.__text);
        this.dialogVisible = false;
        this.getData(this.current - 1);
        if (list.flag == true) {
          this.$message.success("设置成功");
        } else {
          this.$message.success("操作失败");
        }
      });
    },
    //确认新增岗位
    addRole() {
      if (this.newvalue == "") {
        this.$message.error("部门非空");
        return
      }
      if (this.roleValue == "") {
        this.$message.error("岗位等级非空");
        return
      }
      if (this.formInliness.roleName == "") {
        this.$message.error("岗位名称非空");
        return
      }
      this.formInliness.creater = JSON.parse(localStorage.getItem("message")).Id;
      var objs = JSON.stringify(this.formInliness);
      axios(
        "/apis/CEOManage.asmx/Add?index=1&jsondata=" + objs
      ).then(({data}) => {
        var datas = this.$x2js.xml2js(data);
        var list = JSON.parse(datas.string.__text);
        this.dialogVisibles = false;
        this.getData(this.current - 1);
        if (list.flag == true) {
          this.$message.success(list.tip);
        } else {
          this.$message.error(list.tip);
        }
      });
    },
    // 分页
    handleCurrentChange(e) {
      this.getData(e - 1);
    },
    // 选择部门获取departCode
    departChange(e) {
      if (e) {
        this.es = e;
      } else {
        this.es = 0;
      }
    },
    // 新增岗位获取departCode
    newdepartChange(e) {
      if (e) {
        this.formInliness.departId = e;
      } else {
        this.formInliness.departId = 0;
      }
    },
    // 新增岗位获取codeId
    roleChange(e) {
      if (e) {
        this.formInliness.fid = e;
      } else {
        this.formInliness.fid = 0;
      }
    },
    // 部门查询
    onsubmit() {
      this.current = 1;
      this.getData(0);
    },
    //清空
    onclear() {
      this.value = "";
      this.es = 0;
      this.getData(this.es);
    },
    // 获选选中菜单id
    changess(index, b, e, k) {
      console.log(index, b, e, k)
      const arr = this.checkedId[b - 1];
      // console.log(this.checkedId.indexOf(b), "arr")
      if (k == true) {
        if (arr.indexOf(e) < 0) {
          arr.push(e);
        }
      } else {
        if (arr.indexOf(e) < 0 == false) {
          arr.splice(arr.indexOf(e), 1);
        }
      }
      console.log(this.checkedId, "选中的id")
    }
  }
};
</script>

<style lang="scss" scoped>
@import '@/assets/scss/base.scss';

.quanxian {
  padding-left: 10px;
  padding-right: 10px;
  padding-top: 10px;
}

.search {
  height: 50px;
  border-radius: 5px;
  background-color: #fff;
  padding-left: 20px;
  padding-top: 10px;
  margin-bottom: 20px;
}

.table {
  margin-bottom: 20px;
  padding-left: 20px;
  border: 1px solid #f5f5f5;
}

.pgnt {
  text-align: center;
}

.demo-form-inline {
  display: inline-block;
}

.ebtn {
  float: right;
  margin-right: 20px;
}

.color_box {
  width: 5px;
  height: 20px;
  background-color: $Header_color;
  position: absolute;
  top: 22px;
  left: 10px;
}

.dialog-footer {
  text-align: center;
}

.dialog {
  overflow: hidden;
}

.el-dialog__body {
  overflow: auto;
}
</style>
